//
//  HomeMessageCell.swift
//  NDAi
//
//  Created by 丸子 on 2024/6/4.
//

import UIKit

class HomeMessageCell: UITableViewCell {
    var chooseLongPress:( () -> Void)?
    
    lazy var timeLb: UILabel = {
        let label = UILabel()
        label.font = UIFont.pingFangSCRegular(ofSize: 11)
        label.textColor = Theme.color.text_d3
        label.text = "11/19 12:12"
        return label
    }()
    
    lazy var avatarIV: UIImageView = {
        let imageView = UIImageView()
        imageView.allRound(15)
        imageView.image = UIImage(named: "ai_launch_logo")
        return imageView
    }()
    
    lazy var contentV: UIView = {
        let view = UIView()
        view.allRound(8)
        view.backgroundColor = UIColor.white
        return view
    }()
    
//    lazy var pauseBtn: UIButton = {
//        let button = UIButton(type: .custom)
//        button.titleForNormal = "暂停生成"
//        button.titleColorForNormal = Theme.color.text_d1
//        button.titleFont = UIFont.pingFangSCRegular(ofSize: 12)
//        button.backgroundColor = UIColor.white
//        button.allRound(12)
//        button.isHidden = true
//        return button
//    }()
//    
    lazy var contentLb: UILabel = {
        let label = UILabel()
        label.font = UIFont.pingFangSCRegular(ofSize: 15)
        label.textColor = Theme.color.text_d1
        label.text = "啊啊啊啊啊"
        label.numberOfLines = 0
        label.backgroundColor = .clear
        return label
    }()
    
    lazy var errorIV: UIImageView = {
       let imageView = UIImageView()
        imageView.image = UIImage(named: "icon_home_error")
        imageView.isHidden = true
        return imageView
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        selectionStyle = .none
        contentView.addSubviews([timeLb, avatarIV, contentV, errorIV])
        contentV.addSubview(contentLb)

        timeLb.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(8)
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
        }

        avatarIV.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(12)
            make.top.equalTo(timeLb.snp.bottom).offset(10)
            make.width.height.equalTo(30)
        }

        contentLb.setContentHuggingPriority(.required, for: .vertical) // 确保 contentLb 在垂直方向上有优先权
        contentLb.snp.makeConstraints { make in
            make.leading.equalTo(avatarIV.snp.trailing).offset(24)
            make.top.equalTo(avatarIV.snp.top).offset(4)
            make.trailing.equalToSuperview().offset(-8)
            make.bottom.equalToSuperview().offset(-8) // 确保 contentLb 不会超出父视图的底部
//            make.bottom.equalTo(pauseBtn.snp.top).offset(-8)
        }

        contentV.snp.makeConstraints { make in
            make.top.equalTo(contentLb.snp.top).offset(-8)
            make.leading.equalTo(contentLb.snp.leading).offset(-8)
            make.trailing.lessThanOrEqualToSuperview().offset(-55)
            make.bottom.equalTo(contentView.snp.bottom).offset(-8) // 修改这里
        }
        
//        pauseBtn.snp.makeConstraints { make in
//            make.top.equalTo(contentV.snp.bottom).offset(2)
//            make.trailing.equalTo(contentV.snp.trailing).offset(-2)
//            make.height.equalTo(24)
//            make.width.equalTo(90)
//        }
//        
        errorIV.snp.makeConstraints { make in
            make.centerY.equalTo(contentV)
            make.trailing.equalTo(contentV.snp.leading).offset(-10)
            make.width.height.equalTo(24)
        }
        
        let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPressGesture))
        self.contentLb.addGestureRecognizer(longPressGesture)
        self.contentLb.isUserInteractionEnabled = true
    }
    
    @objc func handleLongPressGesture(){
        self.chooseLongPress?()
    }

    
    func setup(message:AIMessage){
        self.timeLb.text = formatTimestamp(message.date)
        self.contentLb.text = message.message ?? ""
        errorIV.isHidden = true
        if message.fromUser {
            if message.state == 3{
                errorIV.isHidden = false
            }
            self.contentLb.textAlignment = .left
            self.updateFromUser()
            
            contentV.backgroundColor = UIColor(hexString: "#25262B")
            contentLb.textColor = .white
            self.avatarIV.image = UIImage(named: "默认头像-页头")
        }else {
            self.contentLb.textAlignment = .left
            self.toUser()
            avatarIV.image = UIImage(named: "ai_launch_logo")
            contentLb.textColor = Theme.color.text_d1
            contentV.backgroundColor = .white
        }
    }
    func formatTimestamp(_ timestamp: TimeInterval) -> String {
        let date = Date(timeIntervalSince1970: timestamp)
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MM/dd HH:mm"
        return dateFormatter.string(from: date)
    }
    func updateFromUser(){
        avatarIV.snp.remakeConstraints { make in
            make.trailing.equalToSuperview().offset(-12) // 修改这里
            make.top.equalTo(timeLb.snp.bottom).offset(10)
            make.width.height.equalTo(30)
        }

        contentLb.setContentHuggingPriority(.required, for: .vertical) // 确保 contentLb 在垂直方向上有优先权
        contentLb.snp.remakeConstraints { make in
            make.trailing.equalTo(avatarIV.snp.leading).offset(-24) // 修改这里
            make.top.equalTo(avatarIV.snp.top).offset(4)
            make.leading.equalToSuperview().offset(8) // 修改这里
            make.bottom.equalToSuperview().offset(-8) // 确保 contentLb 不会超出父视图的底部
        }

        contentV.snp.remakeConstraints { make in
            make.top.equalTo(contentLb.snp.top).offset(-8)
            make.trailing.equalTo(contentLb.snp.trailing).offset(8) // 修改这里
            make.leading.greaterThanOrEqualToSuperview().offset(55) // 修改这里
            make.bottom.equalTo(contentView.snp.bottom).offset(-8)
        }
    }
    
    func toUser(){
        avatarIV.snp.remakeConstraints { make in
            make.leading.equalToSuperview().offset(12)
            make.top.equalTo(timeLb.snp.bottom).offset(10)
            make.width.height.equalTo(30)
        }

        contentLb.setContentHuggingPriority(.required, for: .vertical) // 确保 contentLb 在垂直方向上有优先权
        contentLb.snp.remakeConstraints { make in
            make.leading.equalTo(avatarIV.snp.trailing).offset(24)
            make.top.equalTo(avatarIV.snp.top).offset(4)
            make.trailing.equalToSuperview().offset(-8)
            make.bottom.equalToSuperview().offset(-8) // 确保 contentLb 不会超出父视图的底部
        }

        contentV.snp.remakeConstraints { make in
            make.top.equalTo(contentLb.snp.top).offset(-8)
            make.leading.equalTo(contentLb.snp.leading).offset(-8)
            make.trailing.lessThanOrEqualToSuperview().offset(-55)
            make.bottom.equalTo(contentView.snp.bottom).offset(-8) // 修改这里
        }
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
